package com.sunstar.androidmedia.meidahelper

import android.content.Context
import android.media.AudioAttributes
import android.media.MediaPlayer
import android.net.Uri
import android.os.Build
import android.view.SurfaceHolder
import java.io.File
import java.io.FileOutputStream

class MediaPlayerHelper private constructor(val builder: Builder) {


    private var mPlayer: MediaPlayer? = null

    init {
//        mPlayer = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
//            MediaPlayer.create(
//                builder.context,
//                builder.uri,
//                builder.surfaceHolder,
//                builder.audioAttributes,
//                builder.audioSessionId!!
//            )
//        else MediaPlayer.create(builder.context, builder.uri, builder.surfaceHolder)
//        builder.netPath?.run {
//            mPlayer?.setDataSource(this)
//            mPlayer?.prepareAsync()
//        }
        buildMediaPlayer()
    }

    fun buildMediaPlayer() {
        mPlayer = MediaPlayer()

        mPlayer?.run {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                setAudioAttributes(builder.audioAttributes ?: AudioAttributes.Builder().build())
                if (builder.audioSessionId != null) {
                    audioSessionId = builder.audioSessionId!!
                }

            }
            setDisplay(builder.surfaceHolder!!)
            if (builder.netPath != null)
                setDataSource(builder.netPath)
            if (builder.uri != null)
                setDataSource(builder.context!!, builder.uri!!)
            if (builder.file != null) {
                var fis = FileOutputStream(builder.file)
                var fd = fis.fd
                setDataSource(fd)
                fis.close()
            }
        }
        initListener()
        mPlayer?.prepareAsync()

    }

    fun initListener() {
        mPlayer?.run {
            if (builder.onBufferingUpdateListener != null) setOnBufferingUpdateListener(builder.onBufferingUpdateListener)
            if (builder.onCompletionListener != null) setOnCompletionListener(builder.onCompletionListener)
            if (builder.onPreparedListener != null) setOnPreparedListener(builder.onPreparedListener)
            if (builder.onInfoListener != null) setOnInfoListener(builder.onInfoListener)
            if (builder.onErrorListener != null) setOnErrorListener(builder.onErrorListener)
            if (builder.onSeekCompleteListener != null) setOnSeekCompleteListener(builder.onSeekCompleteListener)
            if (builder.onVideoSizeChangedListener != null) setOnVideoSizeChangedListener(builder.onVideoSizeChangedListener)
        }
    }

    fun resetPlayerSource(path: String) {
        mPlayer?.run {
            setDataSource(path)
            prepareAsync()
        }
    }

    fun resetPlayerSource(uri: Uri) {
        mPlayer?.run {
            setDataSource(builder.context!!, uri)
            prepareAsync()
        }
    }

    fun resetPlayerSource(file: File) {
        val fis = FileOutputStream(builder.file)
        val fd = fis.fd
        mPlayer?.run {
            setDataSource(fd)
            prepareAsync()
        }
        fis.close()
    }

    fun stop() {
        mPlayer?.stop()
    }

    fun start() {
        mPlayer?.start()
    }

    fun pause() {
        mPlayer?.pause()
    }

    fun dispose() {
        mPlayer?.run {
            dispose()
            release()
        }
    }


    class Builder {
        var uri: Uri? = null
        var netPath: String? = null
        var context: Context? = null
        var audioAttributes: AudioAttributes? = null
        var audioSessionId: Int? = null
        var surfaceHolder: SurfaceHolder? = null
        var file: File? = null
        var onBufferingUpdateListener: MediaPlayer.OnBufferingUpdateListener? = null
        var onCompletionListener: MediaPlayer.OnCompletionListener? = null
        var onPreparedListener: MediaPlayer.OnPreparedListener? = null
        var onInfoListener: MediaPlayer.OnInfoListener? = null
        var onErrorListener: MediaPlayer.OnErrorListener? = null
        var onSeekCompleteListener: MediaPlayer.OnSeekCompleteListener? = null
        var onVideoSizeChangedListener: MediaPlayer.OnVideoSizeChangedListener? = null
        fun build(): MediaPlayerHelper {
            return MediaPlayerHelper(this)
        }
    }

}